package com.swjd.jingle.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.swjd.jingle.R;
import com.swjd.jingle.fragment.photopicker.PhotoAdapter;
import com.swjd.jingle.fragment.photopicker.RecyclerItemClickListener;
import com.xuexiang.xui.widget.button.RippleView;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;

import static android.app.Activity.RESULT_OK;

public class IssueFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_desc)
    EditText tvDesc;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.rv_issue)
    RippleView rvIssue;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private PhotoAdapter photoAdapter;

    private ArrayList<String> selectedPhotos = new ArrayList<>();

    @Override
    public void initialize() {
        tvTitle.setText("发布");

        photoAdapter = new PhotoAdapter(activity, selectedPhotos);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.HORIZONTAL));
        recyclerView.setAdapter(photoAdapter);
        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(activity,
                (view, position) -> {
                    if (photoAdapter.getItemViewType(position) == PhotoAdapter.TYPE_ADD) {
                        PhotoPicker.builder()
                                .setPhotoCount(PhotoAdapter.MAX)
                                .setShowCamera(true)
                                .setPreviewEnabled(false)
                                .setSelected(selectedPhotos)
                                .start(activity, IssueFragment.this);
                    } else {
                        PhotoPreview.builder()
                                .setPhotos(selectedPhotos)
                                .setCurrentItem(position)
                                .start(activity, IssueFragment.this);
                    }
                }));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK &&
                (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {

            List<String> photos = null;
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            }
            selectedPhotos.clear();

            if (photos != null) {

                selectedPhotos.addAll(photos);
            }
            photoAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void initViews() {
    }

    @Override
    public void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        if (app.getTypeListBean() != null) {
            tvType.setText(app.getTypeListBean().getTName());
        }
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_issue;
    }

    @Override
    @OnClick({R.id.img_back, R.id.tv_type, R.id.tv_price, R.id.rv_issue})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        fragmentManager.popBackStack();
                    }
                }, 500);
                break;
            case R.id.tv_type:
                SelectTypeFragment selectTypeFragment = new SelectTypeFragment();
                fragmentManager.beginTransaction().replace(R.id.layout_main, selectTypeFragment)
                        .addToBackStack(null)
                        .commit();
                break;
            case R.id.tv_price:
                showInputDialog();
                break;
            case R.id.rv_issue:
                //确认对话框
                DialogLoader.getInstance().showConfirmDialog(activity, "请问是否确认发布", "是", (dialog1, which) -> {

                }, "否");
                break;
        }
    }

    /**
     * 带输入框的对话框
     */
    private void showInputDialog() {
        new MaterialDialog.Builder(getContext())
                .iconRes(R.mipmap.ic_none)
                .title("开个价")
                .content("赶快给闲置的宝贝定义一个价格吧~")
                .inputType(
                        InputType.TYPE_CLASS_NUMBER
                                | InputType.TYPE_TEXT_FLAG_CAP_WORDS)
                .input(
                        getString(R.string.input_price),
                        "",
                        false,
                        (new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {

                            }
                        }))
                .inputRange(1, 10)
                .positiveText("确定")
                .negativeText("取消")
                .onPositive((dia, which) -> IssueFragment.this.tvPrice.setText(dia.getInputEditText().getText().toString()))
                .cancelable(false)
                .show();
    }


}
